JavaScript String match() Method

您所在的位置:网站首页 javascript matches JavaScript String match() Method

JavaScript String match() Method

2024-07-18 01:48:00| 来源: 网络整理| 查看: 265

JavaScript String match() Method: Get Matchings From A String

In this tutorial, you will learn how to match a part of a string and get the matchings from it using regular expressions. We will discuss the JavaScript string match() method.

JavaScript string match JavaScript String match()

The match() method in JavaScript matches a series of characters from a string using a regular expression.

The method accepts a regular expression to find any match. If the match is found then it returns the matching content from the string as an array of strings.

If nothing matches then it returns null.

Example

var str = "Learning To Code Is Learning To Create And Innovate"; var exp = /[A-Z]/g; // Matching capital characters // get matches var matches = str.match(exp); console.log(matches); // output: [ "L", "T", "C", "I", "L", "T", "C", "A", "I" ] ▶ Try It

The above example uses /[A-Z]/g as a regular expression that matches all the capital letters from the string and returns as an array.

Syntax Of match() method

The syntax of the string match() method is following:

string.match(regex); string - It is the string from which the match is to be found. regexp - It is the regular expression to match the string. If the given regexp is not a regular expression then the method implicitly converts it to regex by using the new RegExp(regexp).

Let's see a few examples:

Example 1: non-regex

Example

var str = "learning to code is learning to create and innovate"; // not a regular expression var matches = str.match("to"); console.log(matches); ▶ Try It

In the above example, the passed argument in the match method is not a regular expression so the method implicitly converted it to regex and matched the content.

Example 2: regex

Example

var str = "learning to code is learning to create and innovate"; // regular expression var matches = str.match(/to/g); console.log(matches);

The above example uses /to/g as a regular expression which matches all the to from the string and returns as an array.

Example 3: Nothing passed in the match method

If regexp is not passed then it returns an array with 1 empty string. i.e ['']

Example: no regexp

const str = "learning to code is learning to create and innovate"; const matches = str.match(); // no regexp passed console.log(matches); Return from match() method

The method returns an array of matching content that depends on the presence and absence of global flag (g) in the given regexp and matching contents. Which creates 3 different modes:

regex doesn't have g flag:- If the g flag is not used in regex then the method returns only the first complete match and its capturing group with the index (position of the match) and input (input string).

Example: global flag (g)

const str = "learning to code is learning to create and innovate"; // without g flag const regexp = /l(earn)ing/; const matches = str.match(regexp); console.log(matches); // [ "learning", "earn" ] // Additional information console.log(matches.index); console.log(matches.input); ▶ Try It

regex have g flag:- If the g flag is used in the regex then it returns all the matching within the string without capturing groups and other details, that's why we need the matchAll method.

Example: global flag (g)

const str = "learning to code is learning to create and innovate"; // with g flag const regexp = /l(earn)ing/g; const matches = str.match(regexp); console.log(matches); // [ "learning", "learning" ] ▶ Try It

Nothing matches:- If nothing matches in the string then it always returns null.

Example: Nothing matches

const str = "learning to code is learning to create and innovate"; const regexp = /[0-9]/g; // matching numbers const matches = str.match(regexp); console.log(matches); // null Case sensitivity

One thing to note about regex is that it is case-sensitive.

For example, if you want to match 'to' in an example then it will find for only 'to' not 'To', 'tO' or 'TO'.

Example: case sensitive

const str = "To do or not to do."; const regexp = /to/g; console.log(str.match(regexp));

If you want to get all occurrences of characters or substring regardless of their case then use flag i with regular expression.

Example: using flag i

const str = "To do or not to do."; const regexp = /to/gi; console.log(str.match(regexp)); Using match String In JavaScript # Example 1: Matching all integers

In the following example use the match() function to get all the integers from 0-9.

Match all integers

const str = "Adding 15 an 25 gives 40."; const regexp = /[0-9]/g; // matching numbers const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 2: Ignoring cases

In the following example find the character from the string by ignoring its cases.

Use flag i to ignore cases in regular expression (both upper and lower case selection).

Ignore cases

const str = "ABWTCDXGOHI12355abcvfgthi"; // matching all alphabets const regexp = /[A-Z]/gi; const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 3: Matching a pattern

The following example finds all 'example' followed by 1 or more numeric characters followed by decimals. Like 'example 1.1.1' to 'Example 2.5'

Match all 'example'

const str = "Jump from Example 2.3.5 to example 5.5.1"; // matching all examples const regexp = /(example) \d[(.)\d]*/gi; const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 4: Random search

The following example contains random non-RegExp objects as a parameter to the match() method.

const str = "A random line with Infinity to Infinity and 100, NaN and null too."; console.log(str.match("random")); // ["random"] console.log(str.match(NaN)); // ["NaN"] console.log(str.match(Infinity)); // ["Infinity"] console.log(str.match(+Infinity)); // ["Infinity"] console.log(str.match(100)); // ["100"] console.log(str.match(+100)); // ["100"] console.log(str.match(null)); // ["null"] ▶ Try It string match javascript

The additional properties are:

index: It is the index at which match was found input: It is the search string method is applied upon. groups: It is capturing group whose keys and value are the names and capturing group respectively. Ezoicreport this ad Conclusion

You can use the match() method if you want to get a string or a set of similar strings present in a string. You can use regular expressions or simple strings to match a string. It returns an array of matching elements if no element is matched then returns null.



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭